<template>
  <el-scrollbar :height="scrollHeight?scrollHeight:isTagsview ? `calc(100vh - 154px)` : `calc(100vh - 120px)`">
    <div>
      <zh-form ref="formRef" style="width: 96%" :md="12" :lg="12" :xl="12" :model="tableCenter" :rules="rules"
               label-width="200px">
<!--        <zh-form-input prop="id" label="ID" placeholder="" v-model="tableCenter.id"
                       disabled></zh-form-input>-->
        <zh-form-input prop="projectId" label="所属项目" placeholder=""
                       v-model="tableCenter.projectId"></zh-form-input>
        <zh-form-input prop="datasourceId" label="所属数据源" placeholder=""
                       v-model="tableCenter.datasourceId"></zh-form-input>
        <zh-form-input prop="tableSchema" label="表模式名" placeholder=""
                       v-model="tableCenter.tableSchema"></zh-form-input>
        <zh-form-input prop="tableName" label="表名" placeholder=""
                       v-model="tableCenter.tableName"></zh-form-input>
        <zh-form-input prop="tableFullName" label="表全称" placeholder=""
                       v-model="tableCenter.tableFullName"></zh-form-input>
        <zh-form-select
            prop="tableType"
            label="表类型"
            :options="state.dictData.tableType"
            placeholder="请选择"
            v-model="tableCenter.tableType"
        ></zh-form-select>
        <zh-form-input prop="tableComment" label="表注释" placeholder=""
                       v-model="tableCenter.tableComment"></zh-form-input>
        <zh-form-input prop="topicId" label="所属主题域" placeholder=""
                       v-model="tableCenter.topicId"></zh-form-input>
        <zh-form-select
            prop="boxLevel"
            label="所属数仓层级"
            :options="state.dictData.boxLevel"
            placeholder="请选择"
            v-model="tableCenter.boxLevel"
        ></zh-form-select>
        <zh-form-select
            prop="safelevel"
            label="数据安全等级"
            :options="state.dictData.safelevel"
            placeholder="请选择"
            v-model="tableCenter.safeLevel"
        ></zh-form-select>
        <zh-form-select
            prop="productFrequency"
            label="数据产生频率"
            :options="state.dictData.productFrequency"
            placeholder=""
            v-model="tableCenter.productFrequency"
        ></zh-form-select>
        <zh-form-select
            prop="etlTime"
            label="数据时效性"
            :options="state.dictData.etlTime"
            placeholder=""
            v-model="tableCenter.etlTime"
        ></zh-form-select>
        <zh-form-select
            prop="etlMode"
            label="数据采集方式"
            :options="state.dictData.etlMode"
            placeholder=""
            v-model="tableCenter.etlMode"
        ></zh-form-select>
        <zh-form-select
            prop="valid"
            label="是否有效"
            :options="state.dictData.valid"
            placeholder=""
            v-model="tableCenter.valid"
        ></zh-form-select>
        <zh-form-select
            prop="uniformity"
            label="一致性"
            :options="state.dictData.uniformity"
            placeholder=""
            v-model="tableCenter.uniformity"
        ></zh-form-select>
      </zh-form>
    </div>
  </el-scrollbar>
</template>

<script lang="ts" setup name="DataCenterForm">
import {FormInstance} from 'element-plus';
//------------------接收传入参数------------------------
const props = defineProps({
  scrollHeight: {
    type: String,
    default: ''
  },
  cotablemetaInfo: Object
});

//------------------定义子组件---------------------------
const formRef = ref<FormInstance>();

//------------------内部变量定义-------------------------
const state = reactive({
  hideChecked: false,
  confirmLoading: false,
  dictData: {} as any,
  initData: {},
  appDict: [],
  orgDict: [],
  devOrgDict: [],
  envDict: []
});
const rules = reactive({
  tableId: [
    {
      required: true,
      message: '请输入表Id',
      trigger: 'blur',
    },
  ],
  columnName: [
    {
      required: true,
      message: '请输入字段名',
      trigger: 'blur',
    },
  ],
  dataType: [
    {
      required: true,
      message: '请输入字段类型',
      trigger: 'blur',
    },
  ],
  dataLength: [
    {
      required: true,
      message: '请输入字段长度',
      trigger: 'blur',
    },
  ],
  nullable: [
    {
      required: true,
      message: '请输入是否为空',
      trigger: 'blur',
    },
  ],

});
let tableCenter = reactive<any>({
  projectId: '',
  datasourceId: '',
  tableSchema: '',
  tableName: '',
  tableFullName: '',
  tableType: '',
  tableComment: '',
  topicId: '',
  boxLevel: '',
  safeLevel: '',
  productFrequency: '',
  etlTime: '',
  etlMode: '',
  valid: '',
  uniformity: '',
});
const selectTable = ref({})
const {isTagsview} = storeToRefs($store.theme);
let initForm = {}

watch(
    () => props.cotablemetaInfo,
    (cotablemetaInfo: any) => {
      if (cotablemetaInfo.id) {
        findDataCenterInfo(cotablemetaInfo);
      } else {
        let myForm = Object.assign(initForm, cotablemetaInfo);
        tableCenter = Object.assign(tableCenter, myForm);
        console.log('tableMeta watch', tableCenter);
      }
    },
    {
      deep: true,
    }
);

//------------------定义调用方法-------------------------
function setInitDate(info) {
  formRef.value?.resetFields();
  if (info) {
    tableCenter.value = info;
  }
}

const findDataCenterInfo = (cotablemetaInfo) => {
  const params = {id: cotablemetaInfo.id};
  if (cotablemetaInfo.nodeType == "0") {
    $api.common.send.post('coTableMeta0002', params).then((res) => {
      tableCenter = Object.assign(tableCenter, res.result || {});
      selectTable.value = res.result || {};
      console.log("000000000000", selectTable.value);
    })
  }
}

//保存数据
const saveForm = async () => {
  //校验表单
  let chenckFlag = await formRef.value.checkForm();
  let params = Object.assign({}, tableCenter);
  if (chenckFlag) {
    let saveForm = params;
    console.log('tableCenter.tableType', tableCenter.tableType);
    if (!saveForm.id) {
      let res = await $api.common.send.post('coTableMeta0004', saveForm);
      res.saveType = 'add';
      res.result.nodeType = '0';
      return res;
    } else {
      let res = await $api.common.send.post('coTableMeta0005', saveForm);
      res.saveType = 'edit';
      res.result.nodeType = '0';
      return res;
    }
  }
}

//------------------初始化------------------------------
const params = {
  dictFields: 'tabledatatype,primarykey,nullable,safelevel,desensitization,tableType',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;

});

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({setInitDate, form: formRef, tableCenter, findDataCenterInfo, selectTable, saveForm});
</script>
